package org.jetnuke.modules.calendar;

import org.apache.log4j.Logger;

import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.entities.CalendarEvent;
import org.jetnuke.util.JetUtil;
import org.jetnuke.util.StringUtils;

public class DoAddQuickEvent implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(DoAddQuickEvent.class);

	String title;
	int startMonth;
	int startDay;
	int startYear;
	int startHour;
	int startMin;

	public void setStartMin(int startMin) {
		this.startMin = startMin;
	}

	public void setStartDay(int startDay) {
		this.startDay = startDay;
	}

	public void setStartHour(int startHour) {
		this.startHour = startHour;
	}

	public void setStartMonth(int startMonth) {
		this.startMonth = startMonth;
	}

	public void setStartYear(int startYear) {
		this.startYear = startYear;
	}

	public void setTitle(String title) {
		if (logger.isDebugEnabled()) {
			logger.debug("setTitle(String) - start"); //$NON-NLS-1$
		}

		this.title = StringUtils.filterText(title, true);

		if (logger.isDebugEnabled()) {
			logger.debug("setTitle(String) - end"); //$NON-NLS-1$
		}
	}

	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		if (ui == null || ui.getID() < 2)
			return null;
		CalendarEvent ce = new CalendarEvent();
		ce.setTITLE(title);
		ce.setFULLDESC(" ");
		ce.setUID(ui.getID());
		ce.setLOCATION(" ");
		ce.setRECURRING(0);
		ce.setTYPE(2);// Appointment
		GregorianCalendar start_date = new GregorianCalendar();
		start_date.set(Calendar.YEAR, startYear);
		start_date.set(Calendar.MONTH, startMonth);
		start_date.set(Calendar.DAY_OF_MONTH, startDay);
		ce.setSTART_DATE(start_date.getTime());

		GregorianCalendar start_time = new GregorianCalendar();
		start_time.set(Calendar.HOUR_OF_DAY, startHour);
		start_time.set(Calendar.MINUTE, startMin);
		start_time.set(Calendar.SECOND, 0);
		ce.setSTART_TIME(start_time.getTime());
		GregorianCalendar duration = new GregorianCalendar();
		duration.set(Calendar.HOUR_OF_DAY, 1);
		duration.set(Calendar.MINUTE, 0);
		duration.set(Calendar.SECOND, 0);
		ce.setDURATION(duration.getTime());

		DataAccessDriver.getInst().newCalendarEventDAO().addNew(ce);
		JetUtil.redirect(vc, response, request.getContextPath()
				+ "/do/calendar");

		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return null;
	}
}
